package com.lanou.beautifulwardrobe.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.brand.adapter.BrandListViewAdapter;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ChoiceProduct;
import com.lanou.beautifulwardrobe.entify.ChoiceResult;

import java.util.List;

/**
 * Created by dllo on 15/11/5.
 */
public class BrandShopActivity extends Activity implements View.OnClickListener {

    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ListView shopLv;
    private String id;
    private String title;
    private String imageUrl;
    private BrandListViewAdapter adapter;
    private ImageView shopImage;
    private TextView shopTitle;
    private Button shopcontant,shopcollection;
    private ImageButton shopback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.brand_shop);
        id = getIntent().getStringExtra("id");
        title=getIntent().getStringExtra("title");
        imageUrl=getIntent().getStringExtra("imageUrl");
        initView();
        initData();
    }

    private void initView() {
        shopLv = (ListView) findViewById(R.id.shopLv);
        shopback= (ImageButton) findViewById(R.id.shopback);
        View header = LayoutInflater.from(this).inflate(R.layout.brand_shop_header, null);
        shopImage= (ImageView) header.findViewById(R.id.shopImage);
        shopTitle= (TextView) header.findViewById(R.id.shopTitle);
        shopTitle.setText(title);
        volleySingleton.getImageResuest(imageUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                shopImage.setImageBitmap(response);
            }
        });

        shopLv.addHeaderView(header);
        adapter = new BrandListViewAdapter();
        shopback.setOnClickListener(this);
    }

    private void initData() {

        String shopUrl = Contants.SHOP_URL_PREFIX + id + Contants.SHOP_URL_SUFFIX;
        Log.i("lanou", shopUrl + "///*");
        volleySingleton.getStringRequset(shopUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ChoiceResult choiceResult = gson.fromJson(response, ChoiceResult.class);
                if (choiceResult != null) {
                    final List<ChoiceProduct> choiceProducts = choiceResult.getData().getProducts();
                    adapter.addData(choiceProducts);
                    shopLv.setAdapter(adapter);

                    shopLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String urlId =choiceProducts.get(position-1).getTaobao_num_iid();
                            Intent intent =new Intent(BrandShopActivity.this, ProductDetailActivity.class);
                            intent.putExtra("id",urlId);
                            startActivity(intent);
                        }
                    });
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
